#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "campaignform.h"


#include <QFileDialog>
#include <QMessageBox>
#include <QAction>
#include <QDebug>

namespace
{

enum Action
{
    Action_ADD_SKILL = 0,
    Action_ADD_SPECIAL,
    Action_ADD_CLASS,
    Action_REMOVE
};

}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    QAction* act = ui->mainToolBar->addAction(tr("Add Skill"));
//    act->setData(Action_ADD_SKILL);

//    act = ui->mainToolBar->addAction(tr("Add Special Ability"));
//    act->setData(Action_ADD_SPECIAL);

//    act = ui->mainToolBar->addAction(tr("Add Character Class"));
//    act->setData(Action_ADD_CLASS);

//    act = ui->mainToolBar->addAction(tr("Remove"));
//    act->setData(Action_REMOVE);
}

MainWindow::~MainWindow()
{
    while (ui->tabWidget->count())
    {
        on_tabWidget_tabCloseRequested(0);
    }

    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    while (ui->tabWidget->count())
    {
        on_tabWidget_tabCloseRequested(0);
    }


    QMainWindow::closeEvent(e);
}

void MainWindow::on_actionNew_triggered()
{
    CampaignForm* form = new CampaignForm(this);
    ui->tabWidget->addTab(form, form->campaignName());
    ui->tabWidget->setCurrentWidget(form);
}

void MainWindow::on_actionOpen_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Campaign"), QDir::currentPath(), tr("Campaign File (*.xml)"));
    if (fileName.isEmpty())
    {
        return;
    }

    CampaignForm* form = new CampaignForm(fileName, this);
    ui->tabWidget->addTab(form, form->campaignName());
}

void MainWindow::on_actionSave_triggered()
{
    CampaignForm* form = qobject_cast<CampaignForm*>(ui->tabWidget->currentWidget());

    if (!form)
    {
        return;
    }

    if (form->onSave())
    {
        return;
    }

    on_actionSaveAs_triggered();

}

void MainWindow::on_actionSaveAs_triggered()
{
    CampaignForm* form = qobject_cast<CampaignForm*>(ui->tabWidget->currentWidget());

    if (!form)
    {
        return;
    }

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save"), QDir::currentPath(), tr("Campaign File (*.xml)"));
    if (fileName.isEmpty())
    {
        return;
    }

    if (!form->onSave(fileName))
    {
        qDebug() << Q_FUNC_INFO << "Failed to save as:" << fileName;
    }
}

void MainWindow::on_actionClose_triggered()
{
    int tabIndex = ui->tabWidget->currentIndex();
    on_tabWidget_tabCloseRequested(tabIndex);
}

void MainWindow::on_actionQuit_triggered()
{
    close();
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    /// \todo: check changes

    QWidget* c = ui->tabWidget->currentWidget();
    QWidget* w = ui->tabWidget->widget(index);

    CampaignForm* form = qobject_cast<CampaignForm*>(w);
    if (form && form->campaignChanged())
    {
        if (QMessageBox::question(this,
                                  tr("Save Changes?"),
                                  tr("Do you want to save campaign changes?")) == QMessageBox::Accepted)
        {
            ui->tabWidget->setCurrentWidget(form);

            on_actionSave_triggered();
        }
    }


    ui->tabWidget->removeTab(index);
    if (w)
    {
        w->deleteLater();
    }

    if (c == w)
    {
        return;
    }

    ui->tabWidget->setCurrentWidget(c);

}

void MainWindow::on_tabWidget_currentChanged(int index)
{
    Q_UNUSED(index)

    ui->mainToolBar->clear();

    QWidget* w = ui->tabWidget->currentWidget();
    if (!w)
    {
        return;
    }

    ui->mainToolBar->addActions(w->actions());

}
